define(['jquery', 'bootstrap', 'agent', 'table', 'form'], function ($, undefined, Agent, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/index',
                    add_url: 'shop/add',
                    edit_url: 'shop/edit',
                    // detail_url: 'shop/detail',
                    recharge_url: 'shop/recharge',
                    multi_url: 'shop/multi',
                },
                table: 'shop',
                pageSize: 20, //调整分页大小为20
                pageList: [10, 25, 50, 100, 'All'], //增加一个100的分页大小
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: true,
                visible: true,
                showToggle: false,
                showColumns: false,
                search:false,
                searchFormVisible:true,
                showExport: false,
                columns: [
                    [
                        // {field: 'districts', title: __('省、市、县区'), searchList: function (column) {
                        //         return Template('sourcetpl', {});
                        //     }, formatter: function (value, row, index) {
                        //         return '无';
                        //     }, visible: false
                        // },
                        {field: 'id', title: '商家ID', sortable: true,operate:false},
                        {field: 'shop_name', title: '商家名称', operate: 'LIKE'},
                        {field: 'audit_status', title: '平台审核',formatter: Table.api.formatter.normal,searchList: {0: '待审核', 1: '审核通过',2:'审核失败'}},
                        {field: 'amap_province.name', title: '省份', operate: false},
                        {field: 'amap_city.name', title: '城市', operate: false},
                        {field: 'amap_districts.name', title: '县/区', operate: false},
                        {field: 'address', title: '详细地址', operate: false},
                        {field: 'contacts', title: '联系人', operate: false},
                        {field: 'phone', title: '联系电话', operate: 'LIKE'},
                        {field: 'wechat', title: '微信号', operate: false},
                        {field: 'legal_person', title: '法人', operate: false},
                        {field: 'source', title: '商家来源', formatter: Table.api.formatter.status, custom:{1: '平台分配', 2: '线下发展'},searchList: {1: '平台分配', 2: '线下发展'}},
                        {field: 'money', title: '余额', operate: 'BETWEEN', sortable: true},
                        {field: 'fixed_amount', title:'单次固定充值金额', operate:false,sortable: true},
                        {field: 'invite_qrcode', title: '邀请码', formatter: Table.api.formatter.image, events: Table.api.events.image, operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.toggle ,searchList: {1: '开启', 0: '关闭'}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [

                                // {
                                //     name: 'goods',
                                //     text: __('详情'),
                                //     title: function (row) {
                                //         return `${row.shop_name} 详情`;
                                //     },
                                //     classname: 'btn btn-xs btn-primary btn-dialog',
                                //     icon: 'fa fa-qrcode',
                                //     extend: 'data-area=\'["90%", "80%"]\'',
                                //     url: function (row) {
                                //         return `shop/detail?ids=${row.id}`;
                                //     },
                                //     callback: function (data) {
                                //         $(".btn-refresh").trigger("click"); //刷新数据
                                //     }
                                // },
                                {
                                    name: 'shop',
                                    text: __('成员'),
                                    title: function (row) {
                                        return `${row.shop_name}  成员`;
                                    },
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    icon: 'fa fa-users',
                                    extend: 'data-area=\'["90%", "80%"]\'',
                                    url: function (row) {
                                        return `shop_member/index?ids=${row.id}`;
                                    },
                                    callback: function (data) {
                                        $(".btn-refresh").trigger("click"); //刷新数据
                                    }
                                },
                                {
                                    name: 'goods',
                                    text: __('商品'),
                                    title: function (row) {
                                        return `${row.shop_name}  店铺 商品`;
                                    },
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-shopping-bag',
                                    extend: 'data-area=\'["90%", "80%"]\'',
                                    url: function (row) {
                                        return `shop_goods/index?ids=${row.id}`;
                                    },
                                    callback: function (data) {
                                        $(".btn-refresh").trigger("click"); //刷新数据
                                    }
                                },
                                {
                                    name: '充值记录',
                                    text: '充值记录',
                                    title: '充值记录',
                                    extend: 'data-area=\'["90%", "80%"]\'',
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    url: function (row) {
                                        return 'recharge/index/ids/'+row.id;
                                    }
                                },
                                {
                                    name: 'edit',
                                    text: __('编辑'),
                                    title: function (row) {
                                        return `${row.shop_name} 编辑`;
                                    },
                                    classname: 'btn btn-xs btn-success btn-editone btn-dialog',
                                    icon: 'fa fa-pencil',
                                    extend: 'data-area=\'["90%", "90%"]\'',
                                    url: function (row) {
                                        return `shop/edit?ids=${row.id}`;
                                    },
                                    callback: function (data) {
                                        $(".btn-refresh").trigger("click"); //刷新数据
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        recharge: function () {
            Controller.api.bindevent();
        },
        // detail: function () {
        //     Controller.api.bindevent();
        // },
        api: {
            bindevent: function () {
                $(document).on('click', "input[name='row[status]']", function () {
                    var name = $("input[name='row[name]']");
                    name.prop("placeholder", $(this).val() == 1 ? name.data("placeholder-menu") : name.data("placeholder-node"));
                });
                $("input[name='row[status]']:checked").trigger("click");
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});